package com.demo.webmagic.pipeline;

import java.util.List;

import com.demo.common.constant.ConstantDtoFlight;
import com.demo.common.model.Proxyip;
import com.demo.text.TestProxyIp;
import com.demo.webmagic.dto.DtoProxyIp;
import com.demo.webmagic.pageProcessor.PageProcessorValiProxyIp;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

public class PipelineProxyIp implements Pipeline {

	@Before(Tx.class)
	@Override
	public void process(ResultItems result, Task task) {
		DtoProxyIp dtoProxyIp = result.get("result");
		if(dtoProxyIp!=null && dtoProxyIp.getCode() == ConstantDtoFlight.code_success) {
			List<Proxyip> proxyipList = dtoProxyIp.getProxyIpList();
			for (Proxyip proxyip : proxyipList) {
				//根据ip去数据库中查，如果有则更新时间，如果没有则添加
				Proxyip _proxyip = Proxyip.dao.findFirst("SELECT * FROM proxyip WHERE ipStr = ?", proxyip.getIpStr());
				if(_proxyip == null) {
					proxyip.save();
				} else {
					_proxyip.setValiDate(proxyip.getValiDate());
					_proxyip.update();
				}
			}
			//验证并保存ip
			/*for (Proxyip proxyip : proxyipList) {
				Spider.create(new PageProcessorValiProxyIp(proxyip)).addPipeline(new PipelineValiProxyIp()).addUrl("http://www.ip138.com/ip2city.asp").start();
			}*/
			/*TestProxyIp testProxyIp = new TestProxyIp(proxyipList);
			try {
				proxyipList = testProxyIp.getValidIp();
				for (Proxyip proxyip : proxyipList) {
					//根据ip去数据库中查，如果有则更新时间，如果没有则添加
					Proxyip _proxyip = Proxyip.dao.findFirst("SELECT * FROM proxyip WHERE ipStr = ?", proxyip.getIpStr());
					if(_proxyip == null) {
						proxyip.save();
					} else {
						_proxyip.setValiDate(proxyip.getValiDate());
						_proxyip.update();
					}
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}*/
		} else {
			throw new RuntimeException("访问异常");
		}
	}

}
